package com.aixin.csopequity.repository;

import com.aixin.csopequity.domain.ConfigEquity;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;


public interface EquityRepository extends JpaRepository<ConfigEquity,Long> {

    @Query(value = "SELECT distinct ce.* FROM configequity ce JOIN " +
            " equityleveldef eld ON ce.equityID = eld.equityID " +
            " LEFT JOIN serviceleveldef sld on eld.equityLevelID = sld.equityLevelID " +
            "WHERE ce.equityStatus < 2 " +
            "and (:equityID is  null or ce.equityID = :equityID) " +
            "and (:equityType is null or ce.equityType = :equityType) " +
            "and (:equityLevelID is null or eld.equityLevelID = :equityLevelID) " +
            "and (:serviceID is  null or sld.serviceID = :serviceID) " +
            "order by ce.createTime desc",nativeQuery = true)
    Page<ConfigEquity> getEquity(@Param("equityID") String equityID,
                                 @Param("equityType")  Integer equityType,
                                 @Param("equityLevelID")  String equityLevelID,
                                 @Param("serviceID") String serviceID, Pageable pageable);


//    @Query(value = "SELECT * FROM configequity where equityID = :equityID ",nativeQuery = true)
//    ConfigEquity getEquityByID(@Param("equityID") String equityID);

    Optional<ConfigEquity> findByEquityID(String equityID);


    @Modifying
    @Query(value = "UPDATE configequity  SET equityName = :equityName, " +
            " equityType = :equityType," +
            " equityStatus = :equityStatus, " +
            " remark = :remark, " +
            "updateTime = :updateTime"
            + " where equityID = :equityID" ,nativeQuery = true)
    int updateEquity(@Param("equityID") String equityID,
                     @Param("equityType") int equityType,
                     @Param("equityName") String equityName,
                     @Param("equityStatus") int equityStatus,
                     @Param("remark") String remark,
                     @Param("updateTime") String updateTime);

    Integer deleteByEquityID(String equityID);


}
